/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.power.service.impl;

import me.zhengjie.modules.power.domain.Video;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.power.repository.VideoRepository;
import me.zhengjie.modules.power.service.VideoService;
import me.zhengjie.modules.power.service.dto.VideoDto;
import me.zhengjie.modules.power.service.dto.VideoQueryCriteria;
import me.zhengjie.modules.power.service.mapstruct.VideoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2022-12-06
**/
@Service
@RequiredArgsConstructor
public class VideoServiceImpl implements VideoService {

    private final VideoRepository videoRepository;
    private final VideoMapper videoMapper;

    @Override
    public Map<String,Object> queryAll(VideoQueryCriteria criteria, Pageable pageable){
        Page<Video> page = videoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(videoMapper::toDto));
    }

    @Override
    public List<VideoDto> queryAll(VideoQueryCriteria criteria){
        return videoMapper.toDto(videoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public Video findById(String id) {
           return  videoRepository.findById(id).orElseGet(Video::new);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VideoDto create(Video resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return videoMapper.toDto(videoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Video resources) {
        Video video = videoRepository.findById(resources.getId()).orElseGet(Video::new);
        ValidationUtil.isNull( video.getId(),"Video","id",resources.getId());
        video.copy(resources);
        videoRepository.save(video);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            videoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<VideoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VideoDto video : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("摄像头类型", video.getCameraType());
            map.put("摄像头编码", video.getCameraCode());
            map.put("设备名称", video.getDeviceName());
            map.put("设备IP", video.getDeviceIp());
            map.put("摄像头名称", video.getCameraName());
            map.put("设备编码", video.getDeviceCode());
            map.put("RTSP视频流", video.getRtspVideoStream());
            map.put("接口请求地址", video.getInterfaceIp());
            map.put("视频播放地址", video.getVideoPlayingUrl());
            map.put("经度", video.getLongitude());
            map.put("纬度", video.getLatitude());
            map.put("端口号", video.getPort());
            map.put("登录用户", video.getLoginUser());
            map.put("登录密码", video.getPassword());
            map.put("所属图层", video.getMapId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}